/*
练习结构体的引用的使用
*/
#include <iostream>
#include <string>
struct free_throws
{
    std::string name;
    int made;
    int attemps;
    double percent;
};

void display(const free_throws &f);
void calculatePercent(free_throws &f);
free_throws & accumulate(free_throws &target, const free_throws &source);

int main_strc_ref()
{
    free_throws one = {"Tom", 20, 200};
    free_throws two = {"May", 1, 20};
    free_throws three = {"Joy", 23, 56};
    free_throws four = {"Jack", 36, 230};
    free_throws team = {"England", 0, 0};
    calculatePercent(one);
    calculatePercent(two);
    calculatePercent(four);
    display(one);
    display(two);
    team = accumulate(team, one);
    team = accumulate(team, two);
    display(team);
    // 错误的误操作
    accumulate(team, three) = four;
    std::cout << "误操作之后的结果是: " << std::endl;
    display(team);
    return 0;
}

void display(const free_throws &f) {
    using namespace std;
    cout << "name: " << f.name << " made: " << f.made << " attempts: " << f.attemps << " percent: " << f.percent << endl; 
}

void calculatePercent(free_throws &f) {
    if (f.attemps != 0)
    {
        const float result = (float) f.made / (float)f.attemps;
        std::cout << "calculate result: " << result << std::endl;
        f.percent = 100 * result;
    }else{
        f.percent = 0.0F;
    }
}

free_throws & accumulate(free_throws &target, const free_throws &source) {
    target.attemps = target.attemps + source.attemps;
    target.made = target.made + source.made;
    calculatePercent(target);
    return target;
}
